package com.aaa.service.system.impl;

import cn.hutool.core.util.ObjectUtil;

import com.aaa.dao.system.UserDao;
import com.aaa.entity.system.PageOrder;
import com.aaa.entity.system.User;
import com.aaa.service.system.UserService;
import com.aaa.utils.MyTools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/8/9 10:52
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao ;
//    UserDao userDao = new UserDaoImpl();

    @Override
    public boolean insertUser(User user) {
        //进行加密
        String password = user.getPassword() + "";
        Map<String, String> passwordSalt = MyTools.makePasswordSalt(password);
        user.setPassword(passwordSalt.get("password"));
        user.setSalt(passwordSalt.get("salt"));
        user.setCreateTime(new Date());
        int i = userDao.insertUser(user);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean registerUSer(Map map) {
        String password = map.get("password") + "";
        Map<String, String> passwordSalt = MyTools.makePasswordSalt(password);
        map.put("password", passwordSalt.get("password"));
        map.put("salt", passwordSalt.get("salt"));
        int i = userDao.registerUser(map);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(int[] userid) {
        return userDao.delete(userid);
    }

    @Override
    public boolean updateUser(User user) {
        return userDao.updateUser(user);
    }


    @Override
    public Page queryAll(PageOrder pageOrder,User user) {
        PageHelper.startPage(pageOrder.getPage(), pageOrder.getLimit());
        if(ObjectUtil.isNotEmpty(pageOrder.getField())){
            PageHelper.orderBy(pageOrder.getFieldOrder());
        }
        List<User> list = userDao.queryAll(user);
        Page  pageResult = (Page)list;
        return pageResult;
    }

    @Override
    public boolean queryName(User user) {
        //进行解密
        List<User> list = userDao.queryName(user);
        if (ObjectUtil.isNotEmpty(list)) {
            //获取盐值
            String salt = list.get(0).getSalt() ;
            //获取对应的密文
            String password1 = list.get(0).getPassword();
            //用明文和盐值获取对应的密文
            String password = MyTools.makePasswordSalt(user.getPassword(), salt);
            //如果相等则密码输入正确
            if (password.equals(password1)) {
                return true;
            }
        }
        return false;
    }

  /*  @Override
    public Long queryCount(Map map) {
        return userDao.queryCount(map);
    }
*/

    @Override
    public boolean upadtePassword(User user) {
        //{password=123, method=updatePassword, userid=26}
        String password = user.getPassword();
        Map<String, String> passwordSalt = MyTools.makePasswordSalt(password);
        user.setPassword(passwordSalt.get("password"));
        user.setSalt(passwordSalt.get("salt"));
        return userDao.upadtePassword(user);
    }
    @Override
    public boolean resetPassword(User user) {
        //{password=123, method=updatePassword, userid=26}

        List<User> list = userDao.queryName(user);
        if(ObjectUtil.isNotEmpty(list)){
            String salt = list.get(0).getSalt();
            String password = MyTools.makePasswordSalt(user.getPassword(), salt);
            if(password.equals(list.get(0).getPassword())){
                return false;
            }else {
                Map<String, String> passwordSalt = MyTools.makePasswordSalt(password);
                user.setSalt(passwordSalt.get("salt"));
                user.setPassword(passwordSalt.get("password"));

            }
        }
        return userDao.resetPassword(user);
    }

    @Override
    public User queryUserByName(User user) {
        return userDao.queryUserByName(user);
    }

}